/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 */

#include "posix_threads_thread.hpp"
#include "posix_threads_mutex.hpp"
#include "posix_threads_criticalsection.hpp"
#include "posixcpp_bytearray.hpp"
#include "boundedsynchronizedqueue.hpp"

#include <iostream>
#include <unistd.h>

#define THREADS_NUMBER 8
#define THREADS_NUMBER_HALF (THREADS_NUMBER/2)

// Standart output lock
static posix::threads::Mutex coutLock;

using namespace posixcpp;
using namespace posixcpp::containers;

// C-style thread function
void * threadFunc(void * arg)
{
    int a = (int)arg;

    posix::threads::CriticalSection cs(coutLock);
    std::cout << "Thread " << a << " started\n"
        << "Scheduler policy " << posix::threads::Thread::self().schedPolicy() << std::endl;

    return NULL;
}

class ThreadClass1 : public posix::threads::Thread
{
public:
    ThreadClass1( int number ) : m_number(number) {}
protected:
    void * run() override;
private:
    int m_number;
};

void * ThreadClass1::run()
{
    posix::threads::CriticalSection cs(coutLock);
    std::cout << "Thread " << this->m_number << " started\n"
        << "Scheduler policy " << this->schedPolicy() << std::endl;

    return NULL;
}

class Producer : public posix::threads::Thread
{
public:
    Producer(BoundedSynchronizedQueue<int, 3> &queue) : m_queue(queue) {}
protected:
    void * run() override;
private:
    BoundedSynchronizedQueue<int, 3> &m_queue;
};

class Consumer : public posix::threads::Thread
{
public:
    Consumer(BoundedSynchronizedQueue<int, 3> &queue) : m_queue(queue) {}
protected:
    void * run() override;
private:
    BoundedSynchronizedQueue<int, 3> &m_queue;
};

void * Producer::run()
{
    coutLock.lock();
    std::cout << "[PRODUCER] started" << std::endl;
    coutLock.unlock();
    sleep(1);

    for (int i=0; i<6; ++i)
    {
        m_queue.enqueue(i);
        coutLock.lock();
        std::cout << "[PRODUCER] " << i << " inserted" << std::endl;
        coutLock.unlock();
    }

    return NULL;
}

void * Consumer::run()
{
    coutLock.lock();
    std::cout << "[CONSUMER] started" << std::endl;
    coutLock.unlock();

    int data;
    for (int i=0; i<6; ++i)
    {
        m_queue.dequeue(data);
        coutLock.lock();
        std::cout << "[CONSUMER] " << data << " received" << std::endl;
        coutLock.unlock();
        sleep(1);
    }

    return NULL;
}

int main()
{
    ByteArray( 32, 0xFF );
    // array of pointers to used threads
    posix::threads::Thread *th[THREADS_NUMBER];

    // First half of the threads creted as C-func
    for (int i=0; i<(THREADS_NUMBER_HALF); ++i)
        th[i] = new posix::threads::Thread(threadFunc, (void*)i);
    // Second half are the posix::thread ancestors
    for (int i=THREADS_NUMBER_HALF; i<THREADS_NUMBER; ++i)
        th[i] = new ThreadClass1(i);
    // starting threads
    for (int i=0; i<THREADS_NUMBER; ++i)
        th[i]->start();

    for (int i=0; i<THREADS_NUMBER; ++i)
    {
        th[i]->join();
        posix::threads::CriticalSection cs(coutLock);
        std::cout << "Thread finished" << std::endl;
    }

    BoundedSynchronizedQueue<int, 3> bsqueue(3);
    Producer prod(bsqueue); Consumer cons(bsqueue);
    prod.start(); cons.start();
    prod.join(); cons.join();

    pause();
    return 0;
}
